package com.sunday.scoremanager.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.Constants;
import com.sunday.common.utils.SharePerferenceUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.common.widgets.EmptyLayout;
import com.sunday.common.widgets.adapter.CommonAdapter;
import com.sunday.common.widgets.adapter.ViewHolder;
import com.sunday.common.widgets.recyclerView.HorizontalDividerItemDecoration;
import com.sunday.scoremanager.R;
import com.sunday.scoremanager.adapter.ShopListAdapter;
import com.sunday.scoremanager.base.BaseApp;
import com.sunday.scoremanager.entity.Member;
import com.sunday.scoremanager.entity.Message;
import com.sunday.scoremanager.http.ApiClient;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 刘涛 on 2017/11/16.
 */

public class ShopListActivity extends BaseActivity {

    @Bind(R.id.left_btn)
    ImageView leftBtn;
    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.rightTxt)
    TextView rightTxt;
    @Bind(R.id.right_btn)
    ImageView rightBtn;
    @Bind(R.id.common_header)
    RelativeLayout commonHeader;
    @Bind(R.id.list_view)
    ListView listView;

    private ArrayList<Member> dataSet = new ArrayList<>();
    private ShopListAdapter adapter;
    private boolean fromMine;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_list);
        ButterKnife.bind(this);
        titleView.setText("我的店铺");
        fromMine = getIntent().getBooleanExtra("fromMine", false);
        if (fromMine) {
            getShopList();
        } else {
            dataSet.clear();
            dataSet = getIntent().getParcelableArrayListExtra("memberList");
            initView();
        }

    }

    private void initView() {
        adapter = new ShopListAdapter(mContext, dataSet);
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Member member = dataSet.get(position);
                SharePerferenceUtils.getIns(mContext).saveOAuth(member);
                SharePerferenceUtils.getIns(mContext).putBoolean(Constants.IS_LOGIN, true);
                BaseApp.getInstance().setMember(member);
                intent = new Intent(mContext, MainActivity.class);
                startActivity(intent);
            }
        });
    }

    private void getShopList() {
        long sellerId = BaseApp.getInstance().getMember().getSellerId();
        Call<ResultDO<List<Member>>> call = ApiClient.getApiAdapter().getRelationShop(sellerId);
        call.enqueue(new Callback<ResultDO<List<Member>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<Member>>> call, Response<ResultDO<List<Member>>> response) {
                if (isFinish || response.body() == null) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    if (response.body().getResult() != null) {
                        dataSet.clear();
                        dataSet.addAll(response.body().getResult());
                        initView();
                    }
                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<Member>>> call, Throwable t) {
                ToastUtils.showToast(mContext, R.string.network_error);
            }
        });
    }

}
